/******************************************************************************
* AIR SYSTEMS LABORATORY
* AEROSPACE ENGINEERING
* UNIVERISTY OF TEXAS AT AUSTIN
* UAV FLIGHT SOFTWARE 
* DESCRIPTION:
*   
*
* SOURCE: 
* Orignial: 08/14/2008  Jorge Alvarez joroge@gmail.com
* Modified: 03/20/2009	Sergio García-Nieto sergarro@isa.upv.es
* $Id: serial.c 4 2009-07-21 17:12:11Z joroge@gmail.com $
*************************************************************************************/
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include "serial.h"

/***************************************************************************
 * Open and configure serial port
 ***************************************************************************/
int open_serial(char* serial_port,int baudrate)
{
   int fd;
   struct termios tio_serial;

   /* open serial port */
   fd = open(serial_port, O_RDWR | O_NOCTTY);
   if (fd == -1)
   {
	fprintf(stderr,"open serial: unable to open %s - %s\n",
		serial_port,strerror(errno));
	_exit(-1);
   }

   /* Enable non-blocking IO */
   //fcntl(fd, F_SETFL, O_NONBLOCK);  
      
   /* Serial port setting */
   bzero(&tio_serial, sizeof(tio_serial));
   tio_serial.c_cflag = CS8 | CLOCAL | CREAD;
   tio_serial.c_iflag = IGNBRK | IGNPAR;
   tio_serial.c_oflag = 0;
   //tio_serial.c_lflag = 0;
   
   /******************************************************************************
    * smode = 0, read will be satisfied immediately. The number of characters
    * currently available, or the number of characters requested will be returned.
    * smode > 0, smode sets the number of characters to receive before the read
    * is satisfied.
    ******************************************************************************/
   tio_serial.c_cc[VMIN] = 1;

   cfsetispeed(&tio_serial, baudrate);
   cfsetospeed(&tio_serial, baudrate);

   /* Flush buffer; parameters take effect immediately */
   tcflush(fd, TCIFLUSH);
   tcsetattr(fd, TCSANOW, &tio_serial);
   	


   return (fd);
}

